package tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test {
	public static void main(String argv[]){
		Map<String, Telomere> sample_dict = new HashMap<String,Telomere>();
		File excel = new File("D:/files/ML/4.Python/telomere.xlsx");
		XSSFWorkbook workbook = new XSSFWorkbook();
		InputStream is;
		try {
			is = new FileInputStream(excel);
			workbook = new XSSFWorkbook(is);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			is = null;
		}
		LinearRegression lr_tel = new LinearRegression();
		XSSFSheet table_tel = workbook.getSheetAt(0);
		for (int i = 8; i <= table_tel.getLastRowNum(); i++) {
			XSSFRow row = table_tel.getRow(i);
			XSSFCell sname_cell = row.getCell(1);
			XSSFCell ct_cell = row.getCell(7);
			if(sname_cell != null && !sname_cell.toString().equals("") && ct_cell != null && !ct_cell.toString().equals("")){
				String sname_whole = sname_cell.toString();
				String sname_cut = sname_whole.substring(0,sname_whole.length()-1);
				String ct = ct_cell.toString();
				if(sample_dict.get(sname_cut) == null){
					sample_dict.put(sname_cut, new Telomere(sname_cut));
				}
				sample_dict.get(sname_cut).addCt_tel(Double.valueOf(ct));;
			}
		}
		for (Map.Entry<String, Telomere> entry : sample_dict.entrySet()) {
			String k = entry.getKey();
			Telomere v = entry.getValue();
			List<Double> ld = v.getCt_tel();
			double total = 0;
			for (Double d : ld) {
				total += d;
			}
			v.setCt_mean_tel(total/ld.size());
			if(k.indexOf("con ") == 0){
				v.setCon_log_tel(Math.log(Double.valueOf(k.substring(4))));
				lr_tel.addDataPoint(new DataPoint(v.getCt_mean_tel(), v.getCon_log_tel()));
			}
		}
		lr_tel.run();
		for (Map.Entry<String, Telomere> entry : sample_dict.entrySet()) {
			Telomere v = entry.getValue();
			if(v.getCon_log_tel() <= 0){
				v.setCon_log_tel(v.getCt_mean_tel()*lr_tel.getA1()+lr_tel.getA0());
			}
		}
		
		LinearRegression lr_36b = new LinearRegression();
		XSSFSheet table_36b = workbook.getSheetAt(1);
		for (int i = 8; i <= table_36b.getLastRowNum(); i++) {
			XSSFRow row = table_36b.getRow(i);
			XSSFCell sname_cell = row.getCell(1);
			XSSFCell ct_cell = row.getCell(7);
			if(sname_cell != null && !sname_cell.toString().equals("") && ct_cell != null && !ct_cell.toString().equals("")){
				String sname_whole = sname_cell.toString();
				String sname_cut = sname_whole.substring(0,sname_whole.length()-1);
				String ct = ct_cell.toString();
				if(sample_dict.get(sname_cut) == null){
					sample_dict.put(sname_cut, new Telomere(sname_cut));
				}
				sample_dict.get(sname_cut).addCt_36b(Double.valueOf(ct));;
			}
		}
		for (Map.Entry<String, Telomere> entry : sample_dict.entrySet()) {
			String k = entry.getKey();
			Telomere v = entry.getValue();
			List<Double> ld = v.getCt_36b();
			double total = 0;
			for (Double d : ld) {
				total += d;
			}
			v.setCt_mean_36b(total/ld.size());
			if(k.indexOf("con ") == 0){
				v.setCon_log_36b(Math.log(Double.valueOf(k.substring(4))));
				lr_36b.addDataPoint(new DataPoint(v.getCt_mean_36b(), v.getCon_log_36b()));
			}
		}
		lr_36b.run();
		for (Map.Entry<String, Telomere> entry : sample_dict.entrySet()) {
			Telomere v = entry.getValue();
			if(v.getCon_log_36b() <= 0){
				v.setCon_log_36b(v.getCt_mean_36b()*lr_36b.getA1()+lr_36b.getA0());
			}
			v.setCon_log_delta(v.getCon_log_tel() - v.getCon_log_36b());
		}
		
		for (Map.Entry<String, Telomere> entry : sample_dict.entrySet()) {
			String k = entry.getKey();
			Telomere v = entry.getValue();
			System.out.println(k+'\t'+v.getCon_tel()+'\t'+v.getCon_36b()+'\t'+v.getCon_log_delta());
		}
	}
}
